package us.icitap.commandcenter.ui.operator;


import java.util.List;
import java.util.logging.Level;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.vaadin.artur.icepush.ICEPush;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.CallActionEnum;
import us.icitap.commandcenter.business.CallRolesEnum;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.listeners.OperatorListener;
import us.icitap.commandcenter.ui.custom.CustVerticalLayout;
import us.icitap.commandcenter.ui.dispatcher.CallMessage;
import us.icitap.commandcenter.ui.shared.CallInfoWindow;
import us.icitap.commandcenter.ui.shared.GeneralVars;
import us.icitap.commandcenter.ui.shared.RelatedCallsWindow;
import us.icitap.commandcenter.utils.HandleNotification;
import us.icitap.utils.Result;

import com.vaadin.event.Action;

public class OperatorVerticalLayout extends CustVerticalLayout implements OperatorListener, MessageListener{

	private ICEPush pusher = new ICEPush();
	
	final CallForm callForm = new CallForm(this, null);
	final CallList callList = new CallList();
	 
	
	
	
	private Connection connection;
	private Session session;
	
	public OperatorVerticalLayout() {
		
		this.addComponent(this.callForm);
		
		Result<List<Call>> result = CommandCenterUI.appStatelessBean.getOperatorCallList(CommandCenterUI.getUser().getUsername());
		if(result.isSucceeded()){
			this.callList.addCallList(result.getObject());
		}
		else
			HandleNotification.showErrorOnScreen(result.getMessage());
		
		this.addComponent(this.callList);
		this.setMargin(true);
		this.setSpacing(true);
		
		callList.addActionHandler(new Action.Handler() {
			
			@Override
			public void handleAction(Action action, Object sender, Object target) {
				if (target == null) return;
				Call call = (Call) target;
				
				if (GeneralVars.CORRECT_CALL == action) {
					callForm.resetForm(call, CallActionEnum.UPDATE_CALL);
				}
				else if (GeneralVars.CHANGE_DISPATCHER == action) {
					callForm.resetForm(call, CallActionEnum.CHANGE_DISPATCHER);
				}
				else if (GeneralVars.ADD_CALL == action) {
					callForm.resetForm(call, CallActionEnum.NEW_CALL_RELATED);
				}
				else if (GeneralVars.SHOW_CALL_INFO == action) {
					CallInfoWindow ciW = new CallInfoWindow(call,callList);
                	ciW.show();
				}
					
			}
			@Override
			public Action[] getActions(Object target, Object sender) {
				 if(target == null) return null;
	               return new Action[]{GeneralVars.CORRECT_CALL,GeneralVars.CHANGE_DISPATCHER, GeneralVars.ADD_CALL,GeneralVars.SHOW_CALL_INFO};
			}
		});
		
		this.addExtension(pusher);
		this.startListening();
		
		
		
	}

	public void startListening(){
	    try
	    {
	      connection = CommandCenterUI.appStatelessBean.getConnectionFactory().createConnection();
	      session= connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

	      Destination dest  = session.createTopic("OPERATOR" + CommandCenterUI.getUser().getUsername());
	      MessageConsumer mc = session.createConsumer(dest);
		  mc.setMessageListener(this);
	      connection.start();

	    }
	    catch (JMSException e)
	    {
	      e.printStackTrace();
	    }
	   
	}
	
	@Override
	public void addNewCall(Call call) {
		Result<Call> result = CommandCenterUI.appStatelessBean.addNewCall(call,CallRolesEnum.OPERATOR);

		if(result.isSucceeded()){
			callList.addCall(result.getObject());
			callForm.resetForm();	
		}
		else
			CommandCenterUI.LOG.logp(Level.SEVERE, "CallForm", "actYes", result.getMessage());
		
	}

	@Override
	public void updateCallInformation(Call call) {
		Result<Call> result = CommandCenterUI.appStatelessBean.updateCallInformation(call, CallRolesEnum.OPERATOR);
		if(result.isSucceeded()){
			callList.updateCall(result.getObject());
			callForm.resetForm();	
			HandleNotification.showInfoOnTray("Telefonata u ndryshua");
		}
		else
			HandleNotification.showErrorOnScreen(result.getMessage());
		
	}

	@Override
	public void onMessage(Message message) {
		ObjectMessage objectMessage = (ObjectMessage)message;

		try {
			
			CallMessage callMessage  = (CallMessage) objectMessage.getObject();
			callList.updateCall(callMessage.getCall());
			HandleNotification.showInfoOnTray("Nje telefonate u ndryshua");
			pusher.push();	
	
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		
	}
	
	@Override
	public void detach() {
		CommandCenterUI.LOG.info("DispatcherVerticalLayout.detach()");
		try {
			if(session != null) session.close();
		    if(connection != null) connection.close();
		} catch (JMSException e) {
			e.printStackTrace();
		}
		super.detach();
	}

	@Override
	public void changeDispatcher(Call call) {
		
		Result<Call> result = CommandCenterUI.appStatelessBean.changeDispatcher(call, CallRolesEnum.OPERATOR);
		if(result.isSucceeded()){
			callList.updateCall(result.getObject());
			callForm.resetForm();	
			HandleNotification.showInfoOnTray("Telefonata u ndryshua");
		}
		else
			HandleNotification.showErrorOnScreen(result.getMessage());
	}

	@Override
	public void addNewCallRelated(Call call) {

		Result<Call> result = CommandCenterUI.appStatelessBean.updateCallInformation(call, CallRolesEnum.OPERATOR);

		if(result.isSucceeded()){
			callList.updateCall(result.getObject());
			callForm.resetForm();	
		}
		else
			CommandCenterUI.LOG.logp(Level.SEVERE, "CallForm", "actYes", result.getMessage());
		
	}
}
